{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[知乎](https://zhuanlan.zhihu.com/p/21580949)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div><img src=\"./img/1001.png\", width=500>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:38:00.835419Z",
     "start_time": "2020-02-08T13:38:00.663706Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1., -2.],\n",
       "       [-1.,  0.],\n",
       "       [ 0.,  0.],\n",
       "       [ 2.,  1.],\n",
       "       [ 0.,  1.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "X = np.array([[-1, -2], [-1, 0], [0, 0], [2, 1], [0, 1]], dtype = float )\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:38:06.395985Z",
     "start_time": "2020-02-08T13:38:06.389587Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 2)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m, n = X.shape\n",
    "m, n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:38:13.937751Z",
     "start_time": "2020-02-08T13:38:13.931768Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1., -1.,  0.,  2.,  0.],\n",
       "       [-2.,  0.,  0.,  1.,  1.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:38:20.948614Z",
     "start_time": "2020-02-08T13:38:20.933640Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.2, 0.8],\n",
       "       [0.8, 1.2]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = X.T.dot(X)/m\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div><img src=\"./img/1002.png\", width=500>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:38:45.506132Z",
     "start_time": "2020-02-08T13:38:45.496159Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2. , 0.4]), array([[ 0.70710678, -0.70710678],\n",
       "        [ 0.70710678,  0.70710678]]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lam, c = np.linalg.eig(C)\n",
    "lam, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:39:07.450537Z",
     "start_time": "2020-02-08T13:39:07.446579Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7071067811865475"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/(2**0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:39:34.099579Z",
     "start_time": "2020-02-08T13:39:34.092591Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.70710678, -0.70710678],\n",
       "       [ 0.70710678,  0.70710678]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:39:22.310202Z",
     "start_time": "2020-02-08T13:39:22.305213Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.70710678,  0.70710678],\n",
       "       [-0.70710678,  0.70710678]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P = c.T\n",
    "P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div><img src=\"./img/1003.png\", width=600>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:39:49.157233Z",
     "start_time": "2020-02-08T13:39:49.147256Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2. , -0. ],\n",
       "       [-0. ,  0.4]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(P.dot(C).dot(P.T), decimals = 2) # 保留两位小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-08T13:39:55.698188Z",
     "start_time": "2020-02-08T13:39:55.691167Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2.1213, -0.7071,  0.    ,  2.1213,  0.7071])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = P[0].dot(X.T)\n",
    "np.round(Y, decimals = 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "AMAZING!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
